package auditoryUIbk;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Test extends JFrame {


    // 1 ** JASS sound configuration options
    
    // The audio API used: javasound, directx, rtaudio or asio.
    // Test between javasound or directx. directx requires rtaudio.dll
    static String soundAPI = "javasound";
    
    // JASS's working buffer size.
    // Low values give less lag but may produce audio side effects.
    // Test and use the lowest possible value.
    static int bufferSize = 32;
    
    // Another buffer size. I don't know what it is but keep it low.
    static int bufferSizeRender = 2048;
    
    // Working sample rate. Best when it matches file sample rates.
    // oversampling is bugged.
    static float sampleRate = 11025;




    // 2 ** Demo options
    
    // This is the delay in seconds for playing the spoken option sounds.
    // This allows slightly separating "wheel clicks" from spoken audio.
    static float optiondelay = 0.1f;
    
    // This is a set of option audio files for this demo
    static String[] optionFiles = new String[] {
    	 "ExperimentAudio/andrewaudio/command/experimentinstruction.wav",
    	 "ExperimentAudio/paulaaudio/clothing.wav",
    	 "../fastStereoSound/sounds/options-vegs/cucumber.wav",
    	 "../fastStereoSound/sounds/options-vegs/cucumber2.wav",
    	 "../fastStereoSound/sounds/options-vegs/cucumber.wav",
    	 "../fastStereoSound/sounds/options-vegs/cucumber.wav",
    };
    
   /*
    // This is a set of option audio files for this demo
    static String[] optionFiles = new String[] {
    	 "sounds/options-vegs/bean.wav",
    	 "sounds/options-vegs/beet.wav",
    	 "sounds/options-vegs/corn.wav",
    	 "sounds/options-vegs/cucumber.wav",
    	 "sounds/options-vegs/garlic.wav",
    	 "sounds/options-vegs/onion.wav",
    	 "sounds/options-vegs/pumpkin.wav",
    	 "sounds/options-vegs/spinach.wav",
    };
    */
    
    /*
    // This is a set of option audio files for this demo
    sampleRate = 8000;
    static String[] optionFiles = new String[] {
    	 "sounds/options-airports/Degaulle.wav",
    	 "sounds/options-airports/Madrid.wav",
    	 "sounds/options-airports/Heathrow.wav",
    	 "sounds/options-airports/Jfk.wav",
    	 "sounds/options-airports/Laguardi.wav",
    	 "sounds/options-airports/Oakland.wav",
    	 "sounds/options-airports/Orly.wav",
    	 "sounds/options-airports/Sfairprt.wav",
    };*/
    
    // This is the set of UI (clicks) audio files.
    static String[] UIFiles = new String[] {
    	 "sounds/ui/clickwheel01.wav",
    	 "sounds/ui/clickstart01.wav",
    	 "sounds/ui/clickend01.wav",
    };
    
    // The sound sets we are using for playing audio.
    SoundSet optionSounds, UISounds;
    
    public static void main (String args[]) throws Exception {
        new Test();
    }

    public Test() {
        super();
        initSound();
        initUI();
        setVisible(true);
    }
    
    public void initSound() {
        // First, configure JASS.
        SoundManager.configure(soundAPI, sampleRate, bufferSize, bufferSizeRender);

        // Also make sure JASS will be quitted properly
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                SoundManager.quit();
                System.exit(0);
            }
	      });

        // Build two "option" soundsets
        optionSounds = buildOptionSoundSet(optionFiles);
        
        // Build the "click" soundset
        UISounds = buildUISoundSet(UIFiles);
        
        // Launch JASS player.
        SoundManager.init();
    }
    
    protected SoundSet buildOptionSoundSet(String[] files) {
        int numberOfSounds = files.length;
        int playingChannels = 1; // this will make all option sounds interruptible
        SoundSet optionSounds = new SoundSet("a", numberOfSounds);        
        // Add and spatialize the spoken sounds
        for (int index=0; index<files.length; index++) {
            int location = index%8;
            // locations are: 0=N, 1=NE, 2=E, 3=SE, 4=S, 5=SW, 6=W, 7=NW
            optionSounds.setSample(index, files[index], location, optiondelay);
        }
        return optionSounds;
    }
    
    protected SoundSet buildUISoundSet(String[] files) {
        int numberOfSounds = 10;
        int playingChannels = 5; // UI sounds are additive
        SoundSet UISounds = new SoundSet("b", numberOfSounds);
        // Generate eight spatialized wheel clicks        
        for (int index=0; index<8; index++) {
            int location = index;
            // locations are: 0=N, 1=NE, 2=E, 3=SE, 4=S, 5=SW, 6=W, 7=NW
            UISounds.setSample(index, files[0], location, 0);
        }
        // Set two additional sounds
        UISounds.setSample(8, files[1], 0, 0);
        UISounds.setSample(9, files[2], 0, 0);
        UISounds.setVolume(0.6f);
        return UISounds;
    }
    
    
    
    public void initUI() {
      
      // Test playing sounds.
      JZones zones = new JZones(8, new ZoneListener() {
          public void fingerPressed(int zone) {
              UISounds.play(8);
              if (zone != -1) 
	                optionSounds.play(zone);
          }
          public void zoneChanged(int zone) {
              if (zone != -1)  {
                UISounds.play(zone);
                optionSounds.play(zone);
              }
          }
          public void fingerReleased(int zone) {
              if (zone != -1) 
                  UISounds.play(9);
          }
      });
      
      getContentPane().add(zones, BorderLayout.NORTH);
      pack();
    }
  
}


